package vip.xiaonuo.supplier.modular.base.units.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.base.units.entity.SupplierUnits;
import vip.xiaonuo.supplier.modular.base.units.mapper.SupplierUnitsMapper;
import vip.xiaonuo.supplier.modular.base.units.param.SupplierUnitsAddParam;
import vip.xiaonuo.supplier.modular.base.units.param.SupplierUnitsEditParam;
import vip.xiaonuo.supplier.modular.base.units.param.SupplierUnitsIdParam;
import vip.xiaonuo.supplier.modular.base.units.param.SupplierUnitsPageParam;
import vip.xiaonuo.supplier.modular.base.units.service.SupplierUnitsService;

import java.util.List;

/**
 * 单位Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/02/28 21:16
 **/
@Service
public class SupplierUnitsServiceImpl extends ServiceImpl<SupplierUnitsMapper, SupplierUnits> implements SupplierUnitsService {

    @Override
    public Page<SupplierUnits> page(SupplierUnitsPageParam supplierUnitsPageParam) {
        QueryWrapper<SupplierUnits> queryWrapper = new QueryWrapper<SupplierUnits>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierUnitsPageParam.getUnitName())) {
            queryWrapper.lambda().like(SupplierUnits::getUnitName, supplierUnitsPageParam.getUnitName());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierUnits::getCompanyId,StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierUnitsPageParam.getSortField(), supplierUnitsPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierUnitsPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierUnitsPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierUnitsPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierUnits::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierUnitsAddParam supplierUnitsAddParam) {
        SupplierUnits supplierUnits = BeanUtil.toBean(supplierUnitsAddParam, SupplierUnits.class);
        supplierUnits.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierUnits);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierUnitsEditParam supplierUnitsEditParam) {
        SupplierUnits supplierUnits = this.queryEntity(supplierUnitsEditParam.getId());
        BeanUtil.copyProperties(supplierUnitsEditParam, supplierUnits);
        this.updateById(supplierUnits);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierUnitsIdParam> supplierUnitsIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierUnitsIdParamList, SupplierUnitsIdParam::getId));
    }

    @Override
    public SupplierUnits detail(SupplierUnitsIdParam supplierUnitsIdParam) {
        return this.queryEntity(supplierUnitsIdParam.getId());
    }

    @Override
    public SupplierUnits queryEntity(String id) {
        SupplierUnits supplierUnits = this.getById(id);
        if(ObjectUtil.isEmpty(supplierUnits)) {
            throw new CommonException("单位不存在，id值为：{}", id);
        }
        return supplierUnits;
    }
}
